package unisb.cs.data.tables;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.apache.hadoop.io.Text;

public class TableObjectProxy {

	private String tableClassName;
	private Object record;
	private Class tableClass;
			
	public TableObjectProxy(String name) throws Exception{
		tableClassName = name;
		tableClass = Class.forName(tableClassName);
		record = tableClass.newInstance();
	}
	
	public TableObjectProxy(String name, Record r) throws Exception{
		tableClassName = name;
		tableClass = Class.forName(tableClassName);
		record = r;
	}
	
	public Record getRecord(){
		return (Record)record;
	}
	
	public void setRecord(String tuple) throws Exception { 
		Method m = tableClass.getMethod("setRecord", String.class);
		m.invoke(record, tuple);
	}
	
	public void setRecord(byte[] tuple) throws Exception { 
		Method m = tableClass.getMethod("setRecord", byte[].class);
		m.invoke(record, tuple);
	}
	
	public void setRecord(Record r){
		record = r;
	}
	
	public String getStringRecord() throws Exception {
		Method m = tableClass.getMethod("getStringRecord");
		return (String)m.invoke(record);
	}

	public byte[] getBytes() throws Exception {
		Method m = tableClass.getMethod("getBytes");
		return (byte[])m.invoke(record);
	}
	
	public int getSize() throws Exception {
		Method m = tableClass.getMethod("getSize");
		return (Integer) m.invoke(record);
	}
	
	public byte[] getByteAttribute(int index) throws Exception {
		Method m = tableClass.getMethod("getByteAttribute", int.class);
		return (byte[])m.invoke(record, index);
	}
	
	public String getStringAttribute(int index) throws Exception {
		Method m = tableClass.getMethod("getStringAttribute", int.class);
		return (String)m.invoke(record, index);
	}
	
	public String getTableName() throws Exception {
		Method m = tableClass.getMethod("getTableName");
		return (String)m.invoke(record);
	}
	
	public int getAttributeNumber() throws Exception {
		Method m = tableClass.getMethod("getAttributeNumber");
		return (Integer)m.invoke(record);
	}
	
	public int getAttributeType(int index) throws Exception {
		Method m = tableClass.getMethod("getAttributeType", int.class);
		return (Integer)m.invoke(record, index);
	}
	
	public int getAttributeLength(int index) throws Exception {
		Method m = tableClass.getMethod("getAttributeLength", int.class);
		return (Integer)m.invoke(record, index);
	}
		
	public void getStringAttributes(Text value, int [] valueFieldIndex) throws Exception {
		Method m = tableClass.getMethod("getStringAttributes", Text.class, int[].class);
		m.invoke(record, value, valueFieldIndex);
	}
	
	public String getStringAttributes(int [] valueFieldIndex) throws Exception {
		Method m = tableClass.getMethod("getStringAttributes", int[].class);
		return (String)m.invoke(record, valueFieldIndex);
	}

	public void getByteAttributes(Text key, int keyFieldIndex) throws Exception {
		Method m = tableClass.getMethod("getByteAttributes", Text.class, int.class);
		m.invoke(record, key, keyFieldIndex);
	}
}
